package com.itcast.donkey.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.infterfaces.InMenuOnClickListener;
import com.itcast.donkey.modles.TravelInItemModels;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/9/13.
 */
public class InMenuAdapter extends RecyclerView.Adapter<InMenuAdapter.MenuHolder> {
    private Context context;
    private TravelInItemModels inItemModels;
    private List<TravelInItemModels.TripDaysBean.NodesBean> nodesBeen = new ArrayList<>();
    private InMenuOnClickListener menuOnClickListener;

    public void setMyOnItemClick(InMenuOnClickListener onItemClick) {
        this.menuOnClickListener = onItemClick;
    }

    public InMenuAdapter(Context context, TravelInItemModels inItemModels) {
        this.context = context;
        this.inItemModels = inItemModels;
        for (int i = 0; i < inItemModels.getTrip_days().size(); i++) {
            int day = inItemModels.getTrip_days().get(i).getDay();
            List<TravelInItemModels.TripDaysBean.NodesBean> btemps = inItemModels.getTrip_days().get(i).getNodes();
            for (TravelInItemModels.TripDaysBean.NodesBean b : btemps) {
                b.setTripDay(day);
            }
            nodesBeen.addAll(inItemModels.getTrip_days().get(i).getNodes());
        }
    }

    @Override
    public MenuHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_in_menu, null);
        MenuHolder holder = new MenuHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MenuHolder holder, int position) {
        if (nodesBeen.get(position).getEntry_name() == null) {
            holder.lin_day.setVisibility(View.VISIBLE);
            holder.lin_entry.setVisibility(View.GONE);
//            holder.divide_line.setVisibility(View.GONE);
//            holder.bigdot.setVisibility(View.VISIBLE);
            holder.day.setText("DAY" + nodesBeen.get(position).getTripDay());
        } else {
            holder.lin_day.setVisibility(View.GONE);
            holder.lin_entry.setVisibility(View.VISIBLE);
//            holder.dot.setVisibility(View.VISIBLE);
//            holder.divide_line.setVisibility(View.VISIBLE);
            holder.entry_name.setText(nodesBeen.get(position).getEntry_name() + "");
        }
        if (menuOnClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    menuOnClickListener.onMenuClick(position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        int size = 0;
        for (int i = 0; i < inItemModels.getTrip_days().size(); i++) {
            size += inItemModels.getTrip_days().get(i).getNodes().size();
        }
        return size;
    }

    public class MenuHolder extends RecyclerView.ViewHolder {
        private LinearLayout lin_day;
        private TextView day;
        private TextView entry_name;
        private ImageView bigdot;
        private ImageView divide_line;
        private LinearLayout lin_entry;
        private ImageView dot;

        public MenuHolder(View itemView) {
            super(itemView);
            day = (TextView) itemView.findViewById(R.id.day);
            entry_name = (TextView) itemView.findViewById(R.id.entry_name);
            lin_day = (LinearLayout) itemView.findViewById(R.id.lin_day);
            bigdot = (ImageView) itemView.findViewById(R.id.bigdot);
            divide_line = (ImageView) itemView.findViewById(R.id.divide_line);
            lin_entry = (LinearLayout) itemView.findViewById(R.id.lin_entry);
            dot = (ImageView) itemView.findViewById(R.id.dot);
        }
    }
}
